package com.roger.cmdkit.demo;

public class ThreadLocalTest {

    private static ThreadLocal<Integer> num = new ThreadLocal<>();

    public static void main(String[] arg){
        new ThreadLocalTest().t1();
        new ThreadLocalTest().t2();
    }

    private void t1(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<3;i++){
                    num.set(i);
                    System.out.println(Thread.currentThread().getName() + "==" + num.get());
                    sleep();
                    num.remove();
                }
            }
        }).start();
    }

    private void t2(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=3;i>0;i--){
                    //num.set(i);
                    System.out.println(Thread.currentThread().getName() + "==" + num.get());
                    sleep();
                    num.remove();
                }
            }
        }).start();
    }

    private  void sleep(){
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
